package com.clkj.xiuzheng.controller;

import com.clkj.xiuzheng.dao.FactoryMapper;
import com.clkj.xiuzheng.pojo.Factory;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.service.FactoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/factory")
@Api(value = "/factory", tags = {"厂商管理"})
@CrossOrigin
public class FactoryController {
    @Autowired
    private FactoryService factoryService;

    @ApiOperation(value = "添加厂商")
    @PostMapping("/addFactory")
    public Result addFactory(@RequestBody Factory factory) {
        try {
            factoryService.addFactory(factory);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "修改厂商为下线、上线")
    @PostMapping("/updateFactoryDown")
    public Result updateFactoryDown( Integer factoryId,Integer status) {
        try {
            factoryService.updateFactory(factoryId,status);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
//    @ApiOperation(value = "修改厂商为上线")
//    @PostMapping("/updateFactoryUp")
//    public Result updateFactoryUp(@RequestBody Integer factoryId) {
//        try {
//            factoryService.updateFactoryUp(factoryId);
//            return new Result(true, StatusCode.OK,"修改成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR,"修改失败");
//        }
//    }
    @ApiOperation(value = "查询所有厂商")
    @PostMapping("/getAllFactory")
    public Result getAllFactory(@RequestParam Integer pageSize,
                                @RequestParam Integer pageNum,Integer companyId) {
        try {
            PageInfo pageInfo = factoryService.getAllFactory(pageSize,pageNum,companyId);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据厂商名字查询厂商")
    @PostMapping("/getFactoryByName")
    public Result getFactoryByName(@RequestParam Integer pageSize,
                                   @RequestParam Integer pageNum,
                                   @RequestParam String factoryName,
                                   Integer companyId) {
        try {
            PageInfo pageInfo = factoryService.getFactoryByName(pageSize,pageNum,factoryName, companyId);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }






}
